package com.bw.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.common.MenuTree;
import com.bw.common.Result;
import com.bw.entity.*;
import com.bw.form.RoleQueryForm;
import com.bw.mapper.SysMenuMapper;
import com.bw.mapper.SysRoleMapper;
import com.bw.mapper.SysRoleMenuMapper;
import com.bw.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {


    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public Page getRolePage(Page page, RoleQueryForm query) {

        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(query.getRoleName()), "role_name", query.getRoleName());
        queryWrapper.like(StrUtil.isNotBlank(query.getRoleKey()), "role_key", query.getRoleKey());
        queryWrapper.eq(StrUtil.isNotBlank(query.getStatus()), "status", query.getStatus());
        queryWrapper.eq("del_flag", 0);
        queryWrapper.le(StrUtil.isNotBlank(query.getEndDate()), "create_time", query.getEndDate());
        queryWrapper.ge(StrUtil.isNotBlank(query.getStartDate()), "create_time", query.getStartDate());
        Page selectPage = sysRoleMapper.selectPage(page, queryWrapper);
        return selectPage;
    }

    @Override
    public void addRole(SysRole sysRole) {
        sysRole.setCreateTime(new Date());
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        sysRole.setCreateBy(principal.getUsername());

        sysRoleMapper.insert(sysRole);

    }

    @Override
    public void updateRole(SysRole sysRole) {
        sysRole.setUpdateTime(new Date());
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        sysRole.setUpdateBy(principal.getUsername());
        sysRoleMapper.updateById(sysRole);
    }

    @Override
    public void deleteRole(String ids) {
        sysRoleMapper.deleteRole(ids);

    }

    @Override
    public List<UserMenu> getMenuByRole(Long roleId) {

        // 获取当前角色的菜单
        List<UserMenu> userMenuList = sysRoleMapper.getMenuByRole(roleId);
        List<Long> collect = userMenuList.stream().map(userMenu -> userMenu.getId()).collect(Collectors.toList());
        // 获取所有的菜单
        List<UserMenu> menuAllList = sysMenuMapper.listAll();
        menuAllList.stream().forEach(menu -> {
            if (collect.contains(menu.getId())) {
                menu.setChecked(1);
            } else {
                menu.setChecked(0);
            }
        });
        //构造树形结构
        List<UserMenu> menuList = MenuTree.makeMenuTree(menuAllList, 0l);
        return menuList;
    }

    @Transactional
    @Override
    public void saveRoleMenu(RoleMenuDto roleMenuDto) {
        // 先删后增
        sysRoleMenuMapper.deleteByRoleId(roleMenuDto.getId());
        List<Long> list = roleMenuDto.getList();
        list.forEach(l ->{
            SysRoleMenu sysRoleMenu = SysRoleMenu.builder().roleId(roleMenuDto.getId()).menuId(l).build();
            sysRoleMenuMapper.insert(sysRoleMenu);
        });
    }
}
